package cn.uai.activiti8.approval.service.impl;

import cn.uai.activiti8.approval.bus.AbstractBusinessBus;
import cn.uai.activiti8.approval.service.LeaveApplicationProcessService;
import cn.uai.activiti8.entity.ApplyForLevel;
import cn.uai.activiti8.service.UserService;
import com.alibaba.fastjson2.JSONObject;
import jakarta.annotation.Resource;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @desc 请假申请流程
 * @author uai
 * @since 2025/8/7 21:53
 * @version 1.0
 */
@Service
public class LeaveApplicationProcessServiceImpl extends InfrastructureServiceImpl implements LeaveApplicationProcessService {

    private static final String PROCESS_DEFINITION_ID = "ApplyForLevel-key:6:617503";
    private static final String PROCESS_DEFINITION_KEY = "ApplyForLevel-key";

    @Resource
    private RuntimeService runtimeService;

    @Resource
    private UserService userService;

    @Resource
    private TaskService taskService;

    @Override
    public ProcessInstance start(String body) {
        ApplyForLevel apply = JSONObject.parseObject(body, ApplyForLevel.class);
        Map<String, Object> variables = new HashMap<>();
        dynamicAssignStartVariables(apply, variables);
        return runtimeService.startProcessInstanceById(PROCESS_DEFINITION_ID, variables);
    }

    @Override
    public String complete(String body) {
        return super.complete(body);
    }

    @Override
    public void dynamicAssignStartVariables(Object original, Map<String, Object> variables) {
        ApplyForLevel apply = (ApplyForLevel) original;
        // initiator 是Activiti存储发起人信息的变量名
        variables.put("initiator", apply.getApplicant());
        variables.put("applicant", apply.getApplicant());
        variables.put("type", apply.getType());
        variables.put("reason", apply.getReason());
        variables.put("startDate", apply.getStartDate());
        variables.put("endDate", apply.getEndDate());
        variables.put("assignManager", "681372686954565");
    }
}
